package com.pay.cloud.activity;

import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks;
import androidx.fragment.app.FragmentTransaction;

import com.pay.cloud.Application;
import com.pay.cloud.BindState;
import com.pay.cloud.DataFactory;
import com.pay.cloud.ErrorListener;
import com.pay.cloud.KeyCodeFactory;
import com.pay.cloud.KeyInfo;
import com.pay.cloud.KeyInfoListener;
import com.pay.cloud.Loading;
import com.pay.cloud.NetworkState;
import com.pay.cloud.R;
import com.pay.cloud.SubScreenFactory;
import com.pay.cloud.fragment.TipVerticalFragment;

import static com.pay.cloud.fragment.TipFragment.TipType.ERROR;

public abstract class AbstractActivity extends AppCompatActivity implements OnKeyListener, KeyInfoListener, ErrorListener {
    private FragmentLifecycleCallbacks fragmentLifecycleCallbacks;
    private Loading loading;
    private Toast toast;
    protected Fragment mainFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerFragmentLifecycleCallbacks();
        loading = new Loading(this);
        loading.setOnKeyListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getSupportFragmentManager().unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks);
        loading.dismiss();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;//|View.SYSTEM_UI_FLAG_FULLSCREEN
        window.setAttributes(params);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        KeyInfo keyInfo = KeyCodeFactory.getKeyInfo(keyCode);
        if (null == keyInfo) {
            return false;
        }
        if (keyInfo == KeyInfo.KEY_SEARCH) {
            SubScreenFactory.startSubScreenToWelcome(this);
            Intent intent = new Intent(this, StatisticsActivity.class);
            startActivity(intent);
            return false;
        } else if (keyInfo == KeyInfo.KEY_MENU) {
            SubScreenFactory.startSubScreenToWelcome(this);
            Intent intent = new Intent(this, SettingActivity.class);
            startActivity(intent);
            return false;
        }
        return onKeyDown(keyInfo);
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        KeyInfo keyInfo = KeyCodeFactory.getKeyInfo(keyCode);
        if (null == keyInfo) {
            return false;
        }
        if (keyInfo == KeyInfo.KEY_MENU
                || keyInfo == KeyInfo.KEY_SEARCH
                || keyInfo == KeyInfo.KEY_CANCEL) {
            loading.dismiss();
           return onKeyDown(keyCode, event);
        }
        return false;
    }

    @Override
    public void onError(String msg) {
        setMainFragment(new TipVerticalFragment(ERROR, msg));
    }

    public final void showLoading() {
        loading.show();
    }

    public final void hideLoading() {
        loading.dismiss();
    }

    public final void showToast(String msg) {
        if (null != toast) {
            toast.cancel();
        }
        toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        toast.show();
    }

    public void setMainFragment(Fragment fragment) {
        mainFragment = fragment;
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.disallowAddToBackStack();
        fragmentTransaction.replace(R.id.activity_container, fragment);
        fragmentTransaction.commit();
    }

    private void registerFragmentLifecycleCallbacks() {
        fragmentLifecycleCallbacks = new FragmentLifecycleCallbacks() {
            @Override
            public void onFragmentViewCreated(@NonNull FragmentManager fm, @NonNull Fragment f, @NonNull View v, @Nullable Bundle savedInstanceState) {
                if (f instanceof BindState) {
                    String outMchId = DataFactory.getDataStore().getOutMchId();
                    if (null == outMchId || outMchId.isEmpty()) {
                        ((BindState)f).onError(getString(R.string.please_bind_device_first));
                        Application.speak(getString(R.string.please_bind_device_first));
                        return;
                    }
                }
                if (f instanceof NetworkState) {
                    ((NetworkState)f).run();
                }
            }

        };
        getSupportFragmentManager().registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, true);
    }

}
